﻿using System;
using System.Linq;
using System.Web.Mvc;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Localization;

namespace Hazdesign.TwitterRules.Tokens {
    public interface ITokenProvider : IDependency {
        void Describe(dynamic context);
        void Evaluate(dynamic context);
    }

    public class UrlTokens : ITokenProvider {
        private readonly IContentManager _contentManager;
        private readonly IWorkContextAccessor _workContextAccessor;
        public UrlTokens(IContentManager contentManager, IWorkContextAccessor workContextAccessor) {
            _contentManager = contentManager;
            _workContextAccessor = workContextAccessor;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public void Describe(dynamic context) {
            context.For("Url", T("Url"), T("Tokens for Urls"))
                .Token("Absolute", T("Absolute"), T("Absolute url."))
                .Token("Shorten", T("Shorten"), T("Shorten url."))
                ;
        }

        public void Evaluate(dynamic context){
            Func<string, string> absolute = (url => new UrlHelper(_workContextAccessor.GetContext().HttpContext.Request.RequestContext).ToString());
            Func<string, string> shorten = (url => url.ToUpper());
            Func<string, string> chained = (url => new UrlHelper(_workContextAccessor.GetContext().HttpContext.Request.RequestContext).ToString().ToUpper());
            context.For("Url")
                .Token("Absolute", absolute)
                .Chain("Absolute", "Shorten", chained)
                .Token("Shorten", shorten);
        }
    }
}